<template>
  <el-dialog :title="formData.id ?'编辑角色' : '添加角色'" :visible="showDialog" @close="close">
    <el-form ref="formd" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="角色名称" prop="title">
        <el-input v-model="formData.title" placeholder="请输入角色名称" />
      </el-form-item>
      <el-form-item label="角色描述" prop="description">
        <el-input v-model="formData.description" placeholder="请输入角色描述" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-row>
        <el-button type="primary" size="small" @click="submit">确定</el-button>
        <el-button size="small" @click="close">取消</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { addRole, editRole } from '@/api/setting'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        title: '',
        description: ''
      },
      rules: {
        title: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 新增/编辑角色
    async submit() {
      await this.$refs.formd.validate()
      if (this.formData.id) {
        await editRole(this.formData)
      } else {
        await addRole(this.formData)
      }
      this.$emit('updateEvent')
      this.close()
    },
    // 取消
    close() {
      this.$emit('update:showDialog', false)
      this.formData = {
        title: '',
        description: ''
      }
      this.$refs.formd.resetFields()
    }

  }
}
</script>

<style>

</style>
